#include "tcpclient.h"
#include "ui_tcpclient.h"
#include <QByteArray>
#include <QDebug>
#include <QMessageBox>
#include <QHostAddress>
#include <string.h>
TCPClient::TCPClient(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TCPClient)
{
    ui->setupUi(this);
    resize(500,300);
    loadConfig();

    connect(&m_tcpSocket,SIGNAL(connected())
            ,this,SLOT(showConnect()));
    connect(&m_tcpSocket,SIGNAL(readyRead())
            ,this,SLOT(recvMsg()));

    //连接服务器
    m_tcpSocket.connectToHost(QHostAddress(m_strIP),m_usPORT);
}

TCPClient::~TCPClient()
{
    delete ui;
}

void TCPClient::loadConfig()
{
    QFile file(":/client.config");
    if(file.open(QIODevice::ReadOnly)){
        QByteArray baData = file.readAll();
        QString strData = baData.toStdString().c_str();
        file.close();
        strData.replace("\r\n"," ");
        QStringList strList = strData.split(" ");
        m_strIP = strList.at(0);
        m_usPORT = strList.at(1).toUShort();
        qDebug()<<"ip:"<<m_strIP<<"Port:"<<m_usPORT;
    }else{
        QMessageBox::critical(this,"open config","open config failed");
    }
}

TCPClient &TCPClient::getInstance()
{
    static TCPClient instance;
    return instance;
}

QTcpSocket &TCPClient::getTcpSocket()
{
    return m_tcpSocket;
}

void TCPClient::showConnect()
{
    QMessageBox::information(this,"连接服务器","连接服务器成功");
}

void TCPClient::recvMsg()
{
    qDebug()<<m_tcpSocket.bytesAvailable();
    uint uiPDULen = 0;
    m_tcpSocket.read((char*)&uiPDULen,sizeof(uint));
    uint uiMsgLen = uiPDULen-sizeof(PDU);
    PDU *pdu = mkPdu(uiMsgLen);
    m_tcpSocket.read((char*)pdu+sizeof(uint),uiPDULen-sizeof(uint));
    switch (pdu->uiMsgType) {
    case ENUM_MSG_TYPE_REGIST_RESPOND:{
        if(0==strcmp(pdu->caData,REGIST_OK)){
            QMessageBox::information(this,"注册",REGIST_OK);
        }else if(0==strcmp(pdu->caData,REGIST_FAILED)){
            QMessageBox::warning(this,"注册",REGIST_FAILED);
        }
        break;
    }
    case ENUM_MSG_TYPE_LOGIN_RESPOND:{
        if(0==strcmp(pdu->caData,LOGIN_OK)){
            QMessageBox::information(this,"登录",LOGIN_OK);
            OpeWidget::getInstance().show();
            this->hide();

        }else if(0==strcmp(pdu->caData,LOGIN_FAILED)){
            QMessageBox::warning(this,"登录",LOGIN_FAILED);
        }
        break;
    }
    case ENUM_MSG_TYPE_ALLONLINE_RESPOND:{
        OpeWidget::getInstance().getFriend()->showAllOnlineUsr(pdu);
        break;
    }
    case ENUM_MSG_TYPE_SEARCH_USR_RESPOND:{
        if(0==strcmp(SEARCH_USR_OFFONLINE,pdu->caData)){
            QMessageBox::information(this,"搜索",QString("%1: offonline").arg(OpeWidget::getInstance().getFriend()->m_strSearchName));

        }else if(0==strcmp(SEARCH_USR_ONLINE,pdu->caData)){
            QMessageBox::information(this,"搜索",QString("%1: online").arg(OpeWidget::getInstance().getFriend()->m_strSearchName));

        }else if(0==strcmp(SEARCH_USR_NO,pdu->caData)){
            QMessageBox::information(this,"搜索",QString("%1: not exist").arg(OpeWidget::getInstance().getFriend()->m_strSearchName));
        }
        break;
    }
    default:
        break;
    }
    free(pdu);
    pdu=NULL;
}

// void TCPClient::on_send_pdu_clicked()
// {
//     QString strMsg = ui->lineEdit->text();
//     if(!strMsg.isEmpty()){
//         PDU *pdu = mkPdu(strMsg.size()+1);
//         pdu->uiMsgType = 8888;
//         memcpy(pdu->caMsg,strMsg.toStdString().c_str(),strMsg.size());
//         qDebug()<<(char*)(pdu->caMsg);
//         m_tcpSocket.write((char*)pdu,pdu->uiPDULen);
//         free(pdu);
//         pdu=NULL;
//     }else{
//         QMessageBox::warning(this,"信息发送","发送的信息不能为空");
//     }
// }


void TCPClient::on_login_pb_clicked()
{
    QString strName =ui->name_le->text();
    QString strPwd = ui->pwd_le->text();
    if(!strName.isEmpty()||!strPwd.isEmpty()){
        PDU *pdu = mkPdu(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_LOGIN_REQUEST;
        strncpy(pdu->caData,strName.toStdString().c_str(),32);
        strncpy(pdu->caData+32,strPwd.toStdString().c_str(),32);
        m_tcpSocket.write((char*)pdu,pdu->uiPDULen);
        free(pdu);
        pdu=NULL;
    }else{
        QMessageBox::critical(this,"注册","注册失败：用户名或密码不能为空");
    }
}


void TCPClient::on_regeist_pb_clicked()
{
    QString strName =ui->name_le->text();
    QString strPwd = ui->pwd_le->text();
    if(!strName.isEmpty()||!strPwd.isEmpty()){
        PDU *pdu = mkPdu(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_REGIST_REQUEST;
        strncpy(pdu->caData,strName.toStdString().c_str(),32);
        strncpy(pdu->caData+32,strPwd.toStdString().c_str(),32);
        m_tcpSocket.write((char*)pdu,pdu->uiPDULen);
        free(pdu);
        pdu=NULL;
    }else{
        QMessageBox::critical(this,"登录","注册失败：用户名或密码不能为空");
    }
}


void TCPClient::on_cancel_pb_clicked()
{

}

